package com.ymatou.iapi.couponservice.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.UseCouponBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponCallbackBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponInfo;
import com.ymatou.iapi.couponservice.service.UseCouponCall;
import com.ymatou.iapi.couponservice.service.UseCouponCallbackCall;
import com.ymatou.iapi.couponservice.service.UserReceiveCouponCall;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponTradeRecord;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponTradeRecordWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*用券
*FreeMaker Template自动生成代码
*<!--添加使用记录-->
	insert into ymt_CouponTradeRecord (OrderId, UserId, CouponCode, BatchId, TradeType)
	values (#{orderId,jdbcType=INTEGER},
	#{userId,jdbcType=INTEGER},
	#{couponCode,jdbcType=VARCHAR},
	#{batchId,jdbcType=INTEGER},
	#{tradeType,jdbcType=TINYINT})

		<!--扣减用户优惠券使用剩余次数-->
        UPDATE a SET iCouponUsedCount=iCouponUsedCount-1 from Ymt_CouponPrivateUserBound a with (index  = IX_Ymt_CouponPrivateUserBound_sCouponCode)
        WHERE sCouponCode=#{couponCode, jdbcType=VARCHAR}
        AND iUserId=#{userId, jdbcType=INTEGER}
        AND iCouponUsedCount > 0
        if(@@ROWCOUNT = 0)
        begin
            select 1
            return
        end

		<!--如果是虚拟券则要更新其使用状态-->
        if(exists(select null from Ymt_CouponVirtualRelation with(nolock) where CouponCode = #{couponCode,jdbcType=VARCHAR}))
        begin
            update Ymt_CouponVirtualRelation set IsUsed = 1 where CouponCode = #{couponCode,jdbcType=VARCHAR}
        end

        select 0

        <!--更新批次剩余使用次数（由于setting是公共资源，放到事物外执行）
        UPDATE Ymt_CouponSetting SET iMaxUseTime=iMaxUseTime-1
        WHERE iCouponSettingId IN (SELECT iCouponSettingId FROM Ymt_CouponBatch WITH(NOLOCK) WHERE ibatchid=#{batchId, jdbcType=INTEGER})-->
        
        用劵：1：ymt_couponsetting减1，目前需要调用接口UseCouponCallback  2：Ymt_CouponPrivateUserBound 增加订单mainOrderId  3:Ymt_CouponPrivateUserBound iCouponUsedCount=0，表示没有使用次数
        IsUpdateUsedCount=true
*/
@TestSuite
public class Ts_UseCoupon{
	private static UseCouponBean usecouponBean;
	private static UseCouponCall usecouponCall;
	
	private static UseCouponCallbackBean usecouponcallbackBean;
	private static UseCouponCallbackCall usecouponcallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用券");
	}

	@Before
	public void caseUp() {
		usecouponBean=new UseCouponBean();
		usecouponCall=new UseCouponCall();
		usecouponcallbackBean=new UseCouponCallbackBean();
		usecouponcallbackCall=new UseCouponCallbackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(usecouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCoupon_001() {
		Logger.start(true,"用券-平台券-领取券");
		try {
			String YMDHMS = "yyyy-MM-dd HH:mm";
			SimpleDateFormat sdf = new SimpleDateFormat(YMDHMS);
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			UseCouponInfo couponInfo = new UseCouponInfo();
			couponInfo.setOrderId(orderId);
			couponInfo.setCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
				
//			List<UseCouponInfo> useCouponInfos = new ArrayList<>();
//			useCouponInfos.add(couponInfo);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			usecouponBean.setUserId(userId);
			//usecouponBean.setUseCouponInfos(useCouponInfos);
			usecouponBean.setCouponCodes(CouponCodes);
			usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			//更改使用剩余次数 可使用的次数，使用后减1
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
            usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			usecouponcallbackCall.setData(usecouponcallbackBean);
			usecouponcallbackCall.callService();
			
			Logger.verifyEquals("操作成功！", usecouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", usecouponCall.getStatusCode1(), "验证返回StatusCode");

			Logger.verifyEquals("null", usecouponCall.getSellerId(), "验证返回SellerId");

			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");
			//新增
			Logger.verifyEquals(usecouponBean.getMainOrderId(), privateUserBound2.getMainOrderId(), "验证数据库Ymt_CouponPrivateUserBound中MainOrderId");
			
			Logger.verifyEquals(0, privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券iCouponUsedCount为0");
			Logger.verifyEquals(true, privateUserBound2.getIsUpdateUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券IsUpdateUsedCount为0");
//			for (String useCouponInfo : CouponCodes) {
//				List<YmtCouponTradeRecord> ymtCouponTradeRecords = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,useCouponInfo);
//				
//				Logger.verifyEquals(1, ymtCouponTradeRecords.size(), "验证生成YmtCouponTradeRecord记录数");
//
//				YmtCouponTradeRecord ymtCouponTradeRecord = ymtCouponTradeRecords.get(0);
//				
//				Logger.verifyEquals(batch.getiBatchId(), ymtCouponTradeRecord.getBatchId(), "验证数据库ymt_CouponTradeRecord中iBatchId");
//				Logger.verifyEquals(1, ymtCouponTradeRecord.getTradeType().intValue(), "验证数据库ymt_CouponTradeRecord中TradeType");			
//			}

			
//			for (UseCouponInfo useCouponInfo : useCouponInfos) {
//				List<YmtCouponTradeRecord> ymtCouponTradeRecords = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(useCouponInfo.getOrderId(),useCouponInfo.getCouponCode());
//				
//				Logger.verifyEquals(1, ymtCouponTradeRecords.size(), "验证生成YmtCouponTradeRecord记录数");
//
//				YmtCouponTradeRecord ymtCouponTradeRecord = ymtCouponTradeRecords.get(0);
//				
//				Logger.verifyEquals(batch.getiBatchId(), ymtCouponTradeRecord.getBatchId(), "验证数据库ymt_CouponTradeRecord中iBatchId");
//				Logger.verifyEquals(1, ymtCouponTradeRecord.getTradeType().intValue(), "验证数据库ymt_CouponTradeRecord中TradeType");			
//			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCoupon_002() {
		Logger.start(true,"用券-商品券");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
		
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
		
			UseCouponInfo couponInfo = new UseCouponInfo();
			couponInfo.setOrderId(12234);
			couponInfo.setCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
//			List<UseCouponInfo> useCouponInfos = new ArrayList<>();
//			useCouponInfos.add(couponInfo);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			usecouponBean.setUserId(userId);
			usecouponBean.setCouponCodes(CouponCodes);
			usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			//更改使用剩余次数 可使用的次数，使用后减1
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
            usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			usecouponcallbackCall.setData(usecouponcallbackBean);
			usecouponcallbackCall.callService();
			
			Logger.verifyEquals("操作成功！", usecouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", usecouponCall.getStatusCode1(), "验证返回StatusCode");

			Logger.verifyEquals("null", usecouponCall.getSellerId(), "验证返回StatusCode");
			
			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");
			//新增
			Logger.verifyEquals(usecouponBean.getMainOrderId(), privateUserBound2.getMainOrderId(), "验证数据库Ymt_CouponPrivateUserBound中MainOrderId");
			Logger.verifyEquals(true, privateUserBound2.getIsUpdateUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券IsUpdateUsedCount为0");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCoupon_003() {
		Logger.start(true,"用券-UserId空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			UseCouponInfo couponInfo = new UseCouponInfo();
			couponInfo.setOrderId(12234);
			couponInfo.setCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			usecouponBean.setCouponCodes(CouponCodes);
			usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			Logger.verifyEquals("1", usecouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCoupon_004() {
		Logger.start(true,"用券-OrderId空,默认0");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			//UseCouponInfo couponInfo = new UseCouponInfo();

			//usecouponBean.setMainOrderId(orderId);
			
			//couponInfo.setCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			usecouponBean.setUserId(userId);
			usecouponBean.setCouponCodes(CouponCodes);
			//usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			Logger.verifyEquals("1", usecouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCoupon_005() {
		Logger.start(true,"用券-CouponCode空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			UseCouponInfo couponInfo = new UseCouponInfo();
			couponInfo.setOrderId(12234);
			List<String> CouponCodes=new ArrayList();
			
			
			usecouponBean.setUserId(userId);
		   usecouponBean.setCouponCodes(CouponCodes);
			usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			Logger.verifyEquals("1", usecouponCall.getStatusCode1(), "验证返回StatusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCoupon_006() {
		Logger.start(true,"用券-UserId与券码不匹配");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			UseCouponInfo couponInfo = new UseCouponInfo();
			couponInfo.setOrderId(12234);
			couponInfo.setCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			
			usecouponBean.setUserId(userId+1);
			   usecouponBean.setCouponCodes(CouponCodes);
				usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			Logger.verifyEquals("21002", usecouponCall.getStatusCode1(), "验证返回StatusCode");

			Logger.verifyEquals(true, usecouponCall.getMessage().contains("不存在"), "验证返回StatusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_UseCoupon_007() {
		Logger.start(true,"用券-平台券-领取券");
		try {
			
			int orderId = TestDataManager.getRandomIntNum(10);

//			UseCouponInfo couponInfo = new UseCouponInfo();
//			couponInfo.setOrderId(orderId);
//			couponInfo.setCouponCode("1702215077111537CVHF7NI2");
//			
//			List<UseCouponInfo> useCouponInfos = new ArrayList<>();
//			useCouponInfos.add(couponInfo);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add("1702215077111537CVHF7NI2");
			
			usecouponBean.setUserId(111537);
			   usecouponBean.setCouponCodes(CouponCodes);
				usecouponBean.setMainOrderId(orderId);
			usecouponBean.setAppId("mansit");
			usecouponBean.setRequestId("12344566");
			
			usecouponCall.setData(usecouponBean);
			usecouponCall.callService();	
			
			Logger.verifyEquals("操作成功！", usecouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", usecouponCall.getStatusCode1(), "验证返回StatusCode");

			Logger.verifyEquals("null", usecouponCall.getSellerId(), "验证返回StatusCode");
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}